package cz.muni.fi.pb162.project.geometry;


/**
 * Abstract class for simple (irregular) polygons
 * 
 * @author Martin Pasko
 * @version 05_11_2012
 */
public abstract class SimplePolygon implements Polygon
{
    
    /**
     * Returns the width of the object, 
     * i.e. the width of the smallest bounding rectangle.
     * 
     * @return object's width
     */
    
    public double getWidth() {
        
        double highestX = Double.MIN_VALUE;
        double lowestX = Double.MAX_VALUE;

        for(int i=0; i<getNumVertices(); i++) {
            
            if(getVertex(i).getX() > highestX) {
                highestX = getVertex(i).getX();
            }
            
            if(getVertex(i).getX() < lowestX) {
                lowestX = getVertex(i).getX();
            } 
    }
    return Math.abs(highestX - lowestX);
    }
    
    /**
     * Returns the height of the object, 
     * i.e. the height of the smallest bounding rectangle.
     * 
     * @return object's height
     */
    
    public double getHeight() {
    
        double highestY = Double.MIN_VALUE;
        double lowestY = Double.MAX_VALUE;
        
        for(int i=0; i<getNumVertices(); i++) {
            
           if(getVertex(i).getY() > highestY) {
                highestY = getVertex(i).getY();
            }
            
            if(getVertex(i).getY() < lowestY) {
                lowestY = getVertex(i).getY();
            }
        }
        
        return Math.abs(highestY - lowestY);
    }
    
    /**
     * Returns perimeter of the Simple Polygon including the length of the last indice.
     * 
     * @return perimeter
     */
    
    public double getLength() {
    
        double length = 0;
        for(int i=0; i<getNumVertices(); i++) {
            length += getVertex(i).distance(getVertex(i+1));
        }
        
        return length;
    }  
    
    /**
     * Return area of a simple polygon.
     * 
     * @return Area
     */    
    
    public double getArea() {             
    
        double area = 0;
        for(int i=0; i<getNumVertices(); i++) { 
            area += (getVertex(i).getX()*getVertex(i+1).getY() - getVertex(i+1).getX()*getVertex(i).getY());
        }
        
        return area*0.5;
    }

    public abstract Vertex2D getVertex(int index);

    public abstract int getNumVertices();
    
    /**
     * Returns a string containing the coordinates of vertices.
     * @return string containing the coordinates of vertices.
    */    

    public String toString() {
        
        // StringBuilder sb = new StringBuilder("Polygon: vertices = ");
        
        String s = "Polygon: vertices =";
        for(int i=0; i<getNumVertices(); i++) {
            s += " "+getVertex(i);
            //sb.append(getVertex(i));
            //String s = sb.toString();
        }
        return s;
    }
}
